package com.ylz.sjzx.sio.common.core.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;

/**
 *
 */
@Slf4j
public final class ExecutorPoolUtils {

    /**
     * 核心线程数
     */
    private static final int CORE_POOL_SIZE = 10;
    /**
     * 最大线程数
     */
    private static final int MAXIMUN_POOL_SIZE = 50;
    /**
     * 队列数
     */
    private static final int CAPACITY = 100;
    /**
     * 登录专属线程线程池
     */
    private static ThreadPoolTaskExecutor loginExecutor;

    static {
        log.info("登录业务专属线程池,开始初始化.");
        loginExecutor = new ThreadPoolTaskExecutor();
        loginExecutor.setCorePoolSize(CORE_POOL_SIZE);
        loginExecutor.setMaxPoolSize(MAXIMUN_POOL_SIZE);
        loginExecutor.setQueueCapacity(CAPACITY);
        loginExecutor.setThreadNamePrefix("loginprocess-thread-");
        loginExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
        loginExecutor.initialize();
        log.info("登录业务专属线程池,初始化完成.");
    }

    public static ThreadPoolTaskExecutor getLoginExecutor() {
        return loginExecutor;
    }

    /**
     * @param <V>  泛型
     * @param task 任务
     * @return 结果类型
     */
    public static <V> Future<V> submit(Callable<V> task) {
        return loginExecutor.submit(task);
    }

}
